const api = require('../services/api');

const processChapter = async (id) => {
    let result = await api.getQuestions(id);
    let questions = result?.response?.answer_card;

    let data = [];
    let i = 0;
    for (const item of questions) {
        let { response: { content: questionDetail } } = await api.getQuestionDetial(item.id);
        data.push({
            QuestionType: getType(questionDetail.type),
            QuestionText: questionDetail.topic,
            Anwser: getAnwser(questionDetail.options),
            OptionA: questionDetail.options[0]?.text ?? "",
            OptionB: questionDetail.options[1]?.text ?? "",
            OptionC: questionDetail.options[2]?.text ?? "",
            OptionD: questionDetail.options[3]?.text ?? "",
            OptionE: questionDetail.options[4]?.text ?? "",
            OptionF: questionDetail.options[5]?.text ?? "",
            Difficulty: questionDetail.difficulty,
            Analysis: questionDetail.analysis
        });
    }
    return data;
};

const getType = (type) => {
    return type === "radio" ? "单选题" : "多选题";
}

const getAnwser = (options) => {
    const anwsers = ["A", "B", "C", "D", "E", "F"];
    let anwser = "";
    options.forEach((option, index) => {
        if (option.is_correct) {
            anwser += anwsers[index];
        }
    });
    return anwser;
}

module.exports = {
    processChapter
}